package info.ideatower.compass.service;

import info.ideatower.compass.dao.MenuDao;
import info.ideatower.compass.dao.SystemInfoDao;
import info.ideatower.compass.entity.Menu;
import info.ideatower.compass.entity.SystemInfo;
import info.ideatower.infra.support.web.http.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuService {

    @Autowired
    MenuDao menuDao;

    public Map<String, List<Menu>> getMenuListByName(String sysId) {
        Map<String, List<Menu>> map = new HashMap<>();
        List<Menu> menuList = menuDao.selectBySysId(sysId, "0");
        for (Menu menu : menuList) {
            List<Menu> children = menuDao.selectBySysId(sysId, menu.getId());
            if (!children.isEmpty()) {
                menu.setChildren(children);
            }
        }
        // 分组
        for (Menu menu : menuList) {
            if (StringUtils.isBlank(menu.getGroupName())) {
                List<Menu> temp = map.get(Menu.BLANK_GROUP);
                if (temp == null) {
                    temp = new ArrayList<>();
                    temp.add(menu);
                    map.put(Menu.BLANK_GROUP, temp);
                } else {
                    temp.add(menu);
                }
            } else {
                List<Menu> temp = map.get(menu.getGroupName());
                if (temp == null) {
                    temp = new ArrayList<>();
                    temp.add(menu);
                    map.put(menu.getGroupName(), temp);
                } else {
                    temp.add(menu);
                }
            }

        }
        return map;
    }
}
